package com.yulebaby.callsenter.task;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.lzy.okgo.model.HttpParams;
import com.yulebaby.callsenter.bean.CustomerBean;
import com.yulebaby.callsenter.bean.FindCustomerInSeaBean;
import com.yulebaby.callsenter.global.Constant;
import com.yulebaby.callsenter.http.HttpHelper;
import com.yulebaby.callsenter.http.ServerUrls;
import com.yulebaby.callsenter.http.callback.HttpCallback;
import com.yulebaby.callsenter.http.callback.ResultCallback;
import com.yulebaby.callsenter.ui.activity.LoginActivity;
import com.yulebaby.callsenter.utils.GsonUtil;
import com.yulebaby.callsenter.utils.SPUtil;
import com.yulebaby.callsenter.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: Zhangbo
 * Time: 2016/12/22
 */
public class RequestInternationalWatersCustomerTask {

    /**
     * 获取公海数据
     *
     * @param activity 获取信息的Acitivty
     * @param callback 回调
     */
    public static void doRequestInternationalWatersTask(Activity activity, FindCustomerInSeaBean bean, int datanum, final ResultCallback callback) {
        String user_token = (String) SPUtil.get(Constant.USER_TOKEN, "");
        int user_id = (int) SPUtil.get(Constant.USER_ID, -1);
        if (checkNull(user_token, activity)) {
            HttpParams httpParams = new HttpParams();
            httpParams.put("token", user_token);
            httpParams.put("sale_id", user_id);
            httpParams.put("datanum", datanum);
            if (bean != null) {
                String customer = GsonUtil.parseBeanToJson(bean);
                httpParams.put("customer", customer);
            }
            HttpHelper.getInstance().get(activity, ServerUrls.OPT_FINDCUSTOMERINSEA, httpParams, new HttpCallback<List<CustomerBean>>() {
                @Override
                public void onSuccess(List<CustomerBean> result) {
                    for (CustomerBean customerBean : result) {
                        switch (customerBean.getStage()) {
                            case Constant.Stage.CLIENTCONFIRM:
                                customerBean.setStep("客户确认");
                                break;
                            case Constant.Stage.NEWINROOM:
                                customerBean.setStressIntent(Constant.StressIntent.STATE_NEWINROOM);
                                customerBean.setStep("新入库");
                                continue;
                            case Constant.Stage.COMMUNICATION:
                                customerBean.setStep("沟通中");
                                break;
                            case Constant.Stage.VISITED:
                                customerBean.setStep("已到访");
                                break;
                            case Constant.Stage.SIGNED:
                                customerBean.setStep("已签约");
                                break;
                            case Constant.Stage.RECISSION:
                                customerBean.setStep("已解约");
                                break;
                        }
                        switch (customerBean.getIntention() == null ? "" : customerBean.getIntention()) {
                            case "强烈":
                                customerBean.setStressIntent(Constant.StressIntent.STATE_STRONG);
                                break;
                            case "准强烈":
                                customerBean.setStressIntent(Constant.StressIntent.STATE_MIDDLE);
                                break;
                            case "中等":
                                customerBean.setStressIntent(Constant.StressIntent.STATE_COMMONLY);
                                break;
                            case "较弱":
                                customerBean.setStressIntent(Constant.StressIntent.STATE_LOW);
                                break;
                            default:
                                customerBean.setStressIntent(Constant.StressIntent.STATE_NONE);
                                break;
                        }
                    }
                    callback.onResult(result);
                }

                @Override
                public void onFail(String exception) {
                    super.onFail(exception);
                    callback.onResult(new ArrayList<>());
                }

                @Override
                public void onFailParam(String info) {
                    super.onFailParam(info);
                    callback.onResult(new ArrayList<>());
                }
            });
        }
    }


    /**
     * 判断是否为空s
     *
     * @param string
     * @return
     */
    private static boolean checkNull(String string, Activity mActivity) {
        if (TextUtils.isEmpty(string)) {
            ToastUtil.showToast("登录信息出错,请重新登录");
            //TOKEN 失效跳转到登录界面
            SPUtil.remove(Constant.USER_TOKEN); //对登录信息进行清除
            SPUtil.remove(Constant.USER_ID);
            mActivity.startActivity(new Intent(mActivity, LoginActivity.class));
            mActivity.finish();
            return false;
        }
        return true;
    }
}
